﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Diagnostics;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.IO;
using System.Text;
namespace Piao
{
    [DataContract()]
    public class JsonObject : IEquatable<JsonObject>
    {
        [DataMember(Order = 1)]
        public string start_station_name;
        [DataMember(Order = 2)]
        public string end_station_name;
        [DataMember(Order = 3)]
        public string start_time;
        [DataMember(Order = 4)]
        public string end_time;
        [DataMember(Order = 5)]
        public string @value;
        [DataMember]
        public string id = string.Empty;

        public string lishi = "";
        public string ypInfoDetail = "";
        public string mmstr = "";

        public override string ToString()
        {
            if (!string.IsNullOrEmpty(value))
                return value + "; " + id;
            return base.ToString();
        }

        #region IEquatable<Train> Members

        public bool Equals(JsonObject other)
        {
            return this.value == other.value && this.id == other.id
                && this.start_time == other.start_time;
        }

        #endregion
    }


    public class OrderData : IComparable<OrderData>
    {
        public string station_train_code;
        public string train_date;
        public string seattype_num;
        public string from_station_telecode;
        public string to_station_telecode;
        public string include_student;
        public string from_station_telecode_name;
        public string to_station_telecode_name;
        public string round_train_date;
        public string round_start_time_str;
        public string single_round_type;
        public string train_pass_type;
        public string train_class_arr;
        public string start_time_str;
        public string lishi;
        public string train_start_time = string.Empty;
        public string trainno4;
        public string arrive_time;
        public string from_station_name;
        public string to_station_name;
        public string from_station_no;
        public string to_station_no;
        public string ypInfoDetail;
        public string mmStr;
        public string locationCode;



        public bool HasTicket { get; set; }
        public string Status { get; set; }

        public string ToPostString()
        {
            from_station_telecode_name = Global.StationNameDict[from_station_telecode];
            to_station_telecode_name = Global.StationNameDict[to_station_telecode];

            from_station_name = from_station_telecode_name;
            to_station_name = to_station_telecode_name;


            StringBuilder sb = new StringBuilder();


            var fields = this.GetType().GetFields(System.Reflection.BindingFlags.Instance |
                   System.Reflection.BindingFlags.Public |
                   System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);

            foreach (var f in fields)
            {
                string val = f.GetValue(this) as string;
                if (!string.IsNullOrEmpty(val))
                    val = System.Web.HttpUtility.UrlEncode(val);

                sb.AppendFormat("&{0}={1}", f.Name, val);
            }

            return sb.ToString().Trim('&');
        }




        #region IComparable<OrderData> 成员
        //for sort use. 
        public int CompareTo(OrderData other)
        {
            return string.CompareOrdinal(station_train_code, other.station_train_code);
        }

        #endregion
    }


    public class UserData
    {
        public int PassengerId = 1;

        public UserData(int id)
        {
            this.PassengerId = id;

            passengerTickets = "3,0,1,名字,1,000000198811110033,13800138000,N";
            oldPassengers = "名字,1,000000198811110033";
            passenger_1_seat = "3";
            
            passenger_1_ticket = "1";//成人
            passenger_1_name = "名字";
            passenger_1_cardtype = "1";//二代身份证
            passenger_1_cardno = "000000198811110033";
            passenger_1_mobileno = "18601274730";
            checkbox9 = "N";
        }

        public string passengerTickets { get; set; }
        public string oldPassengers { get; set; }
        public string passenger_1_seat { get; set; }
        //public string passenger_1_seat_detail_select { get; set; }
        //public string passenger_1_seat_detail { get; set; }
        public string passenger_1_ticket { get; set; }
        public string passenger_1_name { get; set; }
        public string passenger_1_cardtype { get; set; }
        public string passenger_1_cardno { get; set; }
        public string passenger_1_mobileno { get; set; }
        public string checkbox9 { get; set; }


        private RequestStringBuilder rsb = new RequestStringBuilder();


        private void UpdateWebForm(Dictionary<string, string> submitDataDict)
        {
            passengerTickets = BuildPassengerTickets();
            oldPassengers = BuildOldPassengers();

            var userFields = this.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
            foreach (var fi in userFields)
            {
                string orderFieldVal = fi.GetValue(this, null) as string;

                string newName = fi.Name.Replace("1", PassengerId.ToString());
                if (submitDataDict.ContainsKey(newName))
                {
                    submitDataDict.Remove(newName);
                }

                rsb[newName] = orderFieldVal;
            }
        }

        public string BuildFormRequestString(Dictionary<string, string> submitDataDict)
        {
            if (PassengerId < 0)
            {
                return string.Empty;
            }

            UpdateWebForm(submitDataDict);


            rsb["passengerTickets"] = BuildPassengerTickets();
            rsb["oldPassengers"] = BuildOldPassengers();

            return rsb.ToString();
        }

        public string BuildEmptyString()
        {
            return "oldPassengers=&checkbox9=Y";
        }

        private string this[string key]
        {
            get
            {
                throw new NotImplementedException();
            }
        }


        private string BuildPassengerTickets()
        {
            string[] arr = 
            {
                passenger_1_seat,
                "undefined",
                passenger_1_ticket,
                passenger_1_name,
                passenger_1_cardtype,
                passenger_1_cardno,
                passenger_1_mobileno,
                checkbox9,
            };

            passengerTickets = string.Join(",", arr);
            return passengerTickets;
        }

        /// <summary>
        /// this is used for auto complete. 
        /// </summary>
        /// <returns></returns>
        private string BuildOldPassengers()
        {
            return string.Format("{0},{1},{2}", passenger_1_name, passenger_1_cardtype, passenger_1_cardno);
        }


    }


}
